package com.project;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.project.facebook.SessionStore;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class ProjectActivity extends Activity {
	ImageButton imgButton;
	Facebook vfacebook;
	public static final String APP_ID = "263600493709730";
	public static final String[] PERMISSIONS = new String[] { "user_photos, publish_checkins,publish_actions,publish_stream" };

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu);
		
//		SessionStore.clear(this);

		Button Button1 = (Button) findViewById(R.id.button1);
		Button1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(v.getContext(), CaptureActivity.class));
			}
		});
		

		Button Button3 = (Button) findViewById(R.id.button3);
		Button3.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				startActivity(new Intent(v.getContext(), ListItem.class));
			}
		});
		imgButton = (ImageButton) findViewById(R.id.buttonFcebook);
		imgButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mFacebookLogIn();
			}
		});
	}

	public void mFacebookLogIn() {
		vfacebook = new Facebook(APP_ID);
		vfacebook.authorize(this, PERMISSIONS, 0,
				new DialogListener() {
					public void onFacebookError(final FacebookError e) {
						Log.e("LOGIN", e.toString());
//						runOnUiThread(new Runnable() {
//							
//							public void run() {
								// TODO Auto-generated method stub
								Toast.makeText(ProjectActivity.this, e.toString(), Toast.LENGTH_LONG).show();
//							}
//						});

					}

					public void onError(final DialogError e) {
						Log.e("LOGIN", e.toString());
						// TODO Auto-generated method stub
//						runOnUiThread(new Runnable() {
//							
//							public void run() {
//								// TODO Auto-generated method stub
								Toast.makeText(ProjectActivity.this, e.toString(), Toast.LENGTH_LONG).show();
//							}
//						});
					}

					public void onComplete(Bundle values) {
						Log.e("TAG", vfacebook.getAccessToken());
//						runOnUiThread(new Runnable() {
//							public void run() {
								SessionStore.save(vfacebook,
										ProjectActivity.this);
								Toast.makeText(ProjectActivity.this,
										"Facebook Login Complate", 0).show();
//							}
//						});
					}

					public void onCancel() {
						Log.e("LOGIN", "Cancel");
						// TODO Auto-generated method stub
//						runOnUiThread(new Runnable() {
//							
//							public void run() {
								// TODO Auto-generated method stub
								Toast.makeText(ProjectActivity.this, "Cancel", Toast.LENGTH_LONG).show();
//							}
//						});
					}
				});
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		vfacebook.authorizeCallback(requestCode, resultCode, data);
	}

}